//
//  BaseBarViewController.m
//  Education
//
//  Created by HUA on 15/7/14.
//  Copyright (c) 2015年 HUA. All rights reserved.
//

#import "BaseBarViewController.h"
#import "LitureTableViewController.h"
#import "MyselfTableViewController.h"
#import "MathViewController.h"
#import "EnglishCollectionViewController.h"
#import "ParadiscViewController.h"
#import "PoemTableViewController.h"
@interface BaseBarViewController ()<UITabBarControllerDelegate>

// 装载三个动画button的view
@property(nonatomic, retain)UIView *myView;

// tabBar中间的button
@property(nonatomic, retain)UIButton *centerButton;

// 三个动画button
@property(nonatomic, retain)UIButton *buttonOne;
@property(nonatomic, retain)UIButton *buttonTwo;
@property(nonatomic, retain)UIButton *buttonThree;

// 是否显示三个动画button
@property(nonatomic, assign)BOOL isShow;
@end


@implementation BaseBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    
    //定义tabar
    
    LitureTableViewController *litureVc = [[LitureTableViewController alloc] init];
    litureVc.title = @"语文";
    [litureVc.tabBarItem setImage:[UIImage imageNamed:@"bt_playpagen_control_order_normal@3x"]];
    UINavigationController *litureNvc = [[UINavigationController alloc] initWithRootViewController:litureVc];
    
    
    MathViewController *mathVc = [[MathViewController alloc] init];
    mathVc.title = @"数学";
    [mathVc.tabBarItem setImage:[UIImage imageNamed:@"bt_playpagen_control_order_normal@3x"]];
    UINavigationController *mathNvc = [[UINavigationController alloc] initWithRootViewController:mathVc];
    
    EnglishCollectionViewController *EnglishVc = [[EnglishCollectionViewController alloc] init];
    EnglishVc.title = @"英语";
    [EnglishVc.tabBarItem setImage:[UIImage imageNamed:@"bt_playpagen_control_order_normal@3x"]];
    UINavigationController *EnglishNvc = [[UINavigationController alloc] initWithRootViewController:EnglishVc];

    ParadiscViewController *ParaVc = [[ParadiscViewController alloc] init];
    ParaVc.title = @"乐园";
    [ParaVc.tabBarItem setImage:[UIImage imageNamed:@"bt_playpagen_control_order_normal@3x"]];
    UINavigationController *ParaNvc = [[UINavigationController alloc] initWithRootViewController:ParaVc];
    
    
    MyselfTableViewController *MyselfVc = [[MyselfTableViewController alloc] init];
    MyselfVc.title = @"我的";
    [MyselfVc.tabBarItem setImage:[UIImage imageNamed:@"bt_playpagen_control_order_normal@3x"]];
    UINavigationController *MyselfNvc = [[UINavigationController alloc] initWithRootViewController:MyselfVc];
    
    self.viewControllers = @[mathNvc,EnglishNvc,litureNvc,ParaNvc,MyselfNvc,];
    
    [self setAnimationView];
        
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -------加载中间按钮动画----
- (void)setAnimationView
{
    
    
    // 创建view 在tabBar下方 此时看不见
    // 装载三个动画button的view
    self.myView = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth / 4, kScreenHeight, 200, 50)];
    // self.myView.backgroundColor = [UIColor redColor];
    self.myView.alpha = 0;
    [self.view addSubview:_myView];
    
    // tabBar中间button
    self.centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.centerButton.frame = CGRectMake((kScreenWidth - 60) / 2, -10, 60, 60);
    self.centerButton.tag = 100;
    [self.centerButton setImage:[UIImage imageNamed:@"28b58PICHyt.jpg"] forState:UIControlStateNormal];
    self.centerButton.layer.masksToBounds = YES;
    [self.centerButton.layer setCornerRadius:30];
    [self.centerButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBar addSubview:_centerButton];
    
    
    // 三个动画button
    self.buttonOne = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOne.frame = CGRectMake(70, 0, 50, 50);
    [self.buttonOne setImage:[UIImage imageNamed:@"28b58PICHyt.jpg"] forState:UIControlStateNormal];
    self.buttonOne.tag = 1001;
    self.buttonOne.layer.masksToBounds = YES;
    [self.buttonOne.layer setCornerRadius:30];
    [self.buttonOne addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:_buttonOne];
    
    self.buttonTwo = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonTwo.frame = CGRectMake(70, 0, 50, 50);
        [self.buttonTwo setImage:[UIImage imageNamed:@"28b58PICHyt.jpg"] forState:UIControlStateNormal];
    self.buttonTwo.tag = 1002;
    self.buttonTwo.layer.masksToBounds = YES;
    [self.buttonTwo.layer setCornerRadius:30];
    [self.buttonTwo addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.buttonTwo setTitle:@"yuwen" forState:UIControlStateNormal];
    [self.myView addSubview:_buttonTwo];
    
    
    self.buttonThree = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonThree.frame = CGRectMake(70, 0, 50, 50);
    [self.buttonThree setImage:[UIImage imageNamed:@"28b58PICHyt.jpg"] forState:UIControlStateNormal];
    self.buttonThree.tag = 1003;
    self.buttonThree.layer.masksToBounds = YES;
    [self.buttonThree.layer setCornerRadius:30];
    [self.buttonThree addTarget:self action:@selector(buttonAction3:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:_buttonThree];
}

// button触发方法
- (void)buttonAction:(UIButton *)button
{
    CABasicAnimation* rotationAnimation3;
    rotationAnimation3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation3.toValue = [NSNumber numberWithFloat: M_PI * 5.0 ];
    rotationAnimation3.duration = 1;
    rotationAnimation3.repeatCount = 1;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDelegate:self];
   
    [UIView commitAnimations];
    
    [UIView animateWithDuration:1 animations:^{
        
        if (_isShow == NO)
        {
            CGRect viewframe = self.myView.frame;
            viewframe.origin.y = kScreenHeight - 120;
            self.myView.frame = viewframe;
            self.myView.alpha = 1;
            
            CGRect buttonOneFrame = self.buttonOne.frame;
            buttonOneFrame.origin.x = 0;
            self.buttonOne.frame = buttonOneFrame;
            
            CGRect buttonTwoFrame = self.buttonTwo.frame;
            buttonTwoFrame.origin.x = kScreenWidth * 1 / 5 - 5 ;
            self.buttonTwo.frame = buttonTwoFrame;
            
            CGRect buttonThreeFrame = self.buttonThree.frame;
            buttonThreeFrame.origin.x = kScreenWidth  * 2 / 5 - 10;
            self.buttonThree.frame = buttonThreeFrame;
            
            
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buttonAction:) name:@"HiddenButton" object:nil];
            
            _isShow = YES;
            
        }
        else
        {
            CGRect viewframe = self.myView.frame;
            viewframe.origin.y = kScreenHeight;
            self.myView.frame = viewframe;
            self.myView.alpha = 0;
            
            CGRect buttonOneFrame = self.buttonOne.frame;
            buttonOneFrame.origin.x = 70;
            self.buttonOne.frame = buttonOneFrame;
            
            CGRect buttonTwoFrame = self.buttonTwo.frame;
            buttonTwoFrame.origin.x = 70;
            self.buttonTwo.frame = buttonTwoFrame;
            
            CGRect buttonThreeFrame = self.buttonThree.frame;
            buttonThreeFrame.origin.x = 70;
            self.buttonThree.frame = buttonThreeFrame;
            
            [[NSNotificationCenter defaultCenter]removeObserver:self name:@"HiddenButton" object:nil];
            
            _isShow = NO;
            
        }
    } completion:nil];
    
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 5.0 ];
    rotationAnimation.duration = 1;
    rotationAnimation.repeatCount = 1;
    
    [self.centerButton.layer addAnimation:rotationAnimation forKey:nil];
    [self.buttonOne.layer addAnimation:rotationAnimation forKey:nil];
    [self.buttonTwo.layer addAnimation:rotationAnimation forKey:nil];
    [self.buttonThree.layer addAnimation:rotationAnimation forKey:nil];
  
    
}
- (void)buttonAction1:(UIButton *)button
{
    [self buttonAction:button];
    LitureTableViewController *litureVc = [[LitureTableViewController alloc] init];
    litureVc.title = @"成语故事";
    UINavigationController *naVC= [[UINavigationController alloc]initWithRootViewController:litureVc];
    UIBarButtonItem *leftbutton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(back)];
    litureVc.navigationItem.leftBarButtonItem = leftbutton;
    [self presentViewController:naVC animated:YES completion:nil];
    
}

- (void)buttonAction3:(UIButton *)button
{
    [self buttonAction:button];
    PoemTableViewController *poemVc = [[PoemTableViewController alloc]init];
    poemVc.title = @"唐诗三百首";
    UINavigationController *naVC= [[UINavigationController alloc]initWithRootViewController:poemVc];
    UIBarButtonItem *leftbutton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(back)];
    poemVc.navigationItem.leftBarButtonItem = leftbutton;
    [self presentViewController:naVC animated:YES completion:nil];
    
}

- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}



@end
